package ext.tianma.part.validators;

import java.util.Locale;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.lifecycle.LifeCycleState;
import wt.lifecycle.State;
import wt.org.WTPrincipal;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.tianma.part.forms.TMTypeUtil;

public class SubmitBomReviewValidator extends DefaultUIComponentValidator{
	
	/**
	 * 天马BOM审核流程只有在零部件为“半成品”和“成品”并且大版本为“0”的情况下才能启动,同时判断当前用户是否对当前对象有“修改”权限
	 */
	@Override
	public UIValidationResultSet performFullPreValidation( UIValidationKey uivalidationKey, UIValidationCriteria uivalidationCriteria, Locale locale) throws WTException {
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		WTPrincipal principal =  SessionHelper.manager.getPrincipal();
		boolean flags = false;
		
		if( principal == null ){
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
			return uivresultSet;
		}
		
		if ( uivalidationCriteria != null ) {
			WTReference wtreference = uivalidationCriteria.getContextObject();
			
			if(null == wtreference){
				uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
				return uivresultSet;
			}
			
			Persistable persistable = wtreference.getObject();
			
			if( persistable != null && persistable instanceof WTPart ){
				WTPart part = ( WTPart )persistable;
				
				if( isAllowPartType( part ) && isAllowPartMajorVersion( part ) && isAllowPartState( part ) && hasPrivilege( principal , part) ){
					flags = true;
				}
			}			
		}
		
		if( flags ){
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.ENABLED));
		}else{
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
		}
		return uivresultSet;
	}
	
	/**
	 * 判断零部件状态
	 * @param part
	 * @return
	 */
	private boolean isAllowPartState(WTPart part) {
		boolean isAllowPartState = false;
		
		if( part == null ){
			return isAllowPartState;
		}
		
		LifeCycleState lifeCycleState =  part.getState();
		if( lifeCycleState != null ){
			
			State state = lifeCycleState.getState();
			if( State.INWORK.getStringValue().equals( state.getStringValue() )){
				isAllowPartState = true;
			}
		}
		return isAllowPartState;
	}
	
	/**
	 * 判断零部件类型
	 * @param part
	 * @return
	 */
	private boolean isAllowPartType(WTPart part) {
		boolean isAllowPartType = false;
		if( part == null ){
			return isAllowPartType;
		}
		
		if( TMTypeUtil.isSemiFinishedGood( part ) || TMTypeUtil.isFinishedGood( part ) ){
			isAllowPartType = true ;
		}
		return false;
	}
	
	/**
	 * 判断零部件大版本
	 * @param part
	 * @return
	 */
	private boolean isAllowPartMajorVersion(WTPart part) {
		boolean isAllowPartMajorVersion = false;
		
		if( part == null ){
			return isAllowPartMajorVersion;
		}
		
		String majorVersion = part.getVersionIdentifier().getValue();
		
		if( majorVersion != null && majorVersion.equals( "0" ) ){
			isAllowPartMajorVersion = true;
		}
		return isAllowPartMajorVersion;
	}
	
	/**
	 * 判断用户是否有设置零部件状态的权限
	 * @param principal
	 * @param part
	 * @return
	 */
	private boolean hasPrivilege( WTPrincipal principal , WTPart part) {
		boolean hasPrivilege = false ;
		
		if( principal == null || part == null ){
			return hasPrivilege;
		}
		
		try {
			hasPrivilege = AccessControlHelper.manager.hasAccess(principal, part, AccessPermission.SET_STATE);		
		} catch (WTException e) {
			e.printStackTrace();
		}
		
		return false;
	}	
}
